/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.suypower.carManage.action;

import com.jarp.db.DAO;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.suypower.carManage.VO.TollsVO;
import com.suypower.carManage.comm.ConstUtil;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import org.apache.commons.lang3.StringUtils;

/**
 *
 * @author hythzx
 */
public class TollsAction extends ActionSupport implements ModelDriven<TollsVO> {

    private TollsVO tollsVO;
    DAO dao = new DAO();

    @Override
    public String execute() {
        String response;
        boolean b = saveOrUpDate();
        if (b) {
            response = SUCCESS;
        } else {
            response = ERROR;
        }
        return response;
    }

    private boolean saveOrUpDate() {
        if (StringUtils.isEmpty(tollsVO.getId())) {
            return this.save();
        } else {
            return this.update();
        }
    }

    private boolean save() {
        boolean b = false;
        String sql = String.format("INSERT INTO dbo.tb_tolls ( car_code, tolls_type, cost, remark, fee_date ) VALUES ( '%s', '%s', %s, '%s', '%s' )", tollsVO.getCar_code(), tollsVO.getTolls_type(), tollsVO.getCost(), tollsVO.getRemark(), tollsVO.getFee_date());
        try {
            int count = dao.execCommand(ConstUtil.DBNAME, sql);
            if (count > 0) {
                b = true;
            }
        } catch (ServletException ex) {
            Logger.getLogger(TollsAction.class.getName()).log(Level.SEVERE, null, ex);
        }
        return b;
    }

    private boolean update() {
        boolean b = false;
        String sql = String.format("UPDATE dbo.tb_tolls SET car_code = '%s', tolls_type = '%s', cost = %s, remark = '%s', fee_date = '%s' where id = %s", tollsVO.getCar_code(), tollsVO.getTolls_type(), tollsVO.getCost(), tollsVO.getRemark(), tollsVO.getFee_date(), tollsVO.getId());
        try {
            int count = dao.execCommand(ConstUtil.DBNAME, sql);
            if (count > 0) {
                b = true;
            }
        } catch (ServletException ex) {
            Logger.getLogger(TollsAction.class.getName()).log(Level.SEVERE, null, ex);
        }
        return b;
    }

    @Override
    public TollsVO getModel() {
        if (tollsVO == null) {
            tollsVO = new TollsVO();
        }
        return tollsVO;
    }
}
